package com.evildog.workspace.system.application.controller;

import com.evildog.workspace.common.object.PageQuery;
import com.evildog.workspace.common.result.PagedResult;
import com.evildog.workspace.common.result.Result;
import com.evildog.workspace.common.result.ResultGenerator;
import com.evildog.workspace.dependencies.mybatis.util.QueryConditionUtils;
import com.evildog.workspace.system.application.bo.UserBO;
import com.evildog.workspace.system.application.bo.UserPageBO;
import com.evildog.workspace.system.application.convert.UserConvert;
import com.evildog.workspace.system.application.model.*;
import com.evildog.workspace.system.application.service.UserService;
import com.evildog.workspace.system.application.vo.UserPageVO;
import com.evildog.workspace.system.application.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * User Controller
    账户表
 * </p>
 *
 * @author evildog
 * @since 2020-07-18
 */
@RestController
@RequestMapping("/users")
@AllArgsConstructor
@Api(tags = "user相关接口")
public class UserController {

    private UserService userService;

    @PostMapping
    @ApiOperation(value = "添加接口")
    public Result<Boolean> save(@Validated @RequestBody UserCreateParam userParam) {
        return ResultGenerator.status(userService.save(UserConvert.INSTANCE.convert(userParam)));
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除接口")
    public Result<Boolean> removeById(@PathVariable("id") Long id) {
        return ResultGenerator.status(userService.remove(id));
    }

    /**
     * 用户自己更新信息接口
     */
    @PutMapping("/{id}")
    @ApiOperation(value = "更新用户自己信息接口")
    public Result<Boolean> updateById(@Validated @RequestBody UserUpdateParam userUpdateParam) {
        return ResultGenerator.status(userService.updateById(UserConvert.INSTANCE.convert(userUpdateParam)));
    }

    /**
     * 用户自己更新密码
     */
    @PatchMapping("/{id}")
    @ApiOperation(value = "更新接口")
    public Result<Boolean> updatePassword(@Validated @RequestBody UserUpdatePassParam userUpdateParam) {
        return ResultGenerator.status(userService.updatePassword(UserConvert.INSTANCE.convert(userUpdateParam)));
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "查询详情接口")
    public Result<UserVO> getById(@PathVariable("id") Long id) {
        UserBO userBO = userService.getDetailById(id);
        return ResultGenerator.success(UserConvert.INSTANCE.convert(userBO));
    }

    @GetMapping("/page")
    @ApiOperation(value = "表分页查询接口")
    public Result<PagedResult<UserPageVO>> getPageList(UserPageParam userPageParam, PageQuery query){
        PagedResult<UserPageBO> userPageBO = userService.getPageList(
                        query,
                        QueryConditionUtils.getQueryCondition(userPageParam),
                        UserConvert.INSTANCE.convert(userPageParam));
        return ResultGenerator.success(UserConvert.INSTANCE.convert(userPageBO));
    }

    @PutMapping("/{id}/user-groups")
    @ApiOperation(value = "更新用户关联的用户组")
    public Result<Boolean> updateUserGroup(@PathVariable("id") Long id,@RequestBody UserUpdateGroupParam userParam) {
        return ResultGenerator.status(userService.updateUserGroup(id, UserConvert.INSTANCE.convert(userParam)));
    }

}
